package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class PanelDate extends Panel {
	public static final int JOUR_CHANGE = 8888;
	private String tabMois [] = new String[] {"Janvier", "F\u00E9vrier", "Mars", "Avril", "Mai", "Juin", "Juillet", 
			  "Aout", "Septembre", "Octobre", "Novembre", "D\u00E9cembre"};

	private Calendar cal = new GregorianCalendar();
	
	private JLabel lblDateDarrivee,
					lblDateDeDepart, 
					lblJourArr,
					lblMoisArr,
					lblAnneeArr,
					lblAnneeDep,
					lblJourDep,
					lblMoisDep;
	private JSpinner jspJourArr,
						jspAnneArr,
						jspJourDep,
						jspAnneeDep;
	private SpinnerNumberModel  modelJourArr,
								modelAnneeArr,
								modelJourDep,
								modelAnneeDep;
	private JComboBox comboBoxMoisArr,
						comboBoxMoisDep;
	public PanelDate(final ActionListener ctrl){
		super();

		modelJourArr = new SpinnerNumberModel(Calendar.DAY_OF_MONTH, 1, 31,1);
		modelJourDep = new SpinnerNumberModel(Calendar.DAY_OF_MONTH +2, 1, 31,1);
		modelAnneeArr = new SpinnerNumberModel(cal.get(Calendar.YEAR), cal.get(Calendar.YEAR), 2100, 1);
		modelAnneeDep = new SpinnerNumberModel(cal.get(Calendar.YEAR), cal.get(Calendar.YEAR), 2100, 1);
		
		this.setBackground(new Color(244, 247, 252));
		this.setBounds(0, 186, 532, 59);
		this.setLayout(null);
				
		lblDateDarrivee = new JLabel("Date d'arrivee");
		lblDateDarrivee.setFont(new Font("Bookman Old Style", Font.BOLD, 12));
		lblDateDarrivee.setBounds(10, 10, 151, 14);
		this.add(lblDateDarrivee);
		
		lblDateDeDepart = new JLabel("Date de depart");
		lblDateDeDepart.setFont(new Font("Bookman Old Style", Font.BOLD, 12));
		lblDateDeDepart.setBounds(10, 34, 128, 14);
		this.add(lblDateDeDepart);
		
		lblJourArr = new JLabel("Jour");
		lblJourArr.setFont(new Font("Bookman Old Style", Font.BOLD, 12));
		lblJourArr.setBounds(167, 10, 41, 16);
		this.add(lblJourArr);

		jspJourArr = new JSpinner();
		jspJourArr.setModel(modelJourArr);
		jspJourArr.setValue(cal.get(Calendar.DAY_OF_MONTH));
		jspJourArr.setBounds(200, 11, 35, 20);
		jspJourArr.addChangeListener(new DateChangeListener(ctrl, jspJourArr));
		this.add(jspJourArr);
		
		lblMoisArr = new JLabel("mois");
		lblMoisArr.setFont(new Font("Bookman Old Style", Font.BOLD, 12));
		lblMoisArr.setBounds(269, 10, 52, 16);
		this.add(lblMoisArr);
		
		comboBoxMoisArr = new JComboBox();
		comboBoxMoisArr.setModel(new DefaultComboBoxModel(tabMois));
		comboBoxMoisArr.setSelectedIndex(cal.get(Calendar.MONTH));
		comboBoxMoisArr.setBounds(313, 10, 76, 20);
		comboBoxMoisArr.addActionListener(ctrl);
		comboBoxMoisArr.setActionCommand(String.valueOf(JOUR_CHANGE));
		this.add(comboBoxMoisArr);
			
		lblAnneeArr = new JLabel("Ann\u00E9e");
		lblAnneeArr.setFont(new Font("Bookman Old Style", Font.BOLD, 12));
		lblAnneeArr.setBounds(420, 10, 52, 16);
		this.add(lblAnneeArr);
		
		jspAnneArr = new JSpinner();
		jspAnneArr.setModel(modelAnneeArr);
		jspAnneArr.setBounds(465, 10, 57, 20);
		jspAnneArr.addChangeListener(new DateChangeListener(ctrl, jspAnneArr));
		this.add(jspAnneArr);
		
		lblJourDep = new JLabel("Jour");
		lblJourDep.setFont(new Font("Bookman Old Style", Font.BOLD, 12));
		lblJourDep.setBounds(167, 34, 41, 16);
		this.add(lblJourDep);
		
		jspJourDep = new JSpinner();
		jspJourDep.setModel(modelJourDep);
		jspJourDep.setValue(cal.get(Calendar.DAY_OF_MONTH)+2);
		jspJourDep.setBounds(200, 34, 35, 20);
		jspJourDep.addChangeListener(new DateChangeListener(ctrl, jspJourDep));
		this.add(jspJourDep);
		
		lblMoisDep = new JLabel("mois");
		lblMoisDep.setFont(new Font("Bookman Old Style", Font.BOLD, 12));
		lblMoisDep.setBounds(269, 34, 52, 16);
		this.add(lblMoisDep);
		
		comboBoxMoisDep = new JComboBox();
		comboBoxMoisDep.setModel(new DefaultComboBoxModel(tabMois));
		comboBoxMoisDep.setBounds(313, 34, 76, 20);
		comboBoxMoisDep.setSelectedIndex(cal.get(Calendar.MONTH));
		comboBoxMoisDep.addActionListener(ctrl);
		comboBoxMoisDep.setActionCommand(String.valueOf(JOUR_CHANGE));
		this.add(comboBoxMoisDep);
		setBackground(new Color(204, 204, 255));
				
		lblAnneeDep = new JLabel("Ann\u00E9e");
		lblAnneeDep.setFont(new Font("Bookman Old Style", Font.BOLD, 12));
		lblAnneeDep.setBounds(420, 34, 52, 16);
		this.add(lblAnneeDep);
		
		jspAnneeDep = new JSpinner();
		jspAnneeDep.setModel(modelAnneeDep);
		jspAnneeDep.setBounds(465, 34, 57, 20);
		jspAnneeDep.addChangeListener(new DateChangeListener(ctrl, jspAnneeDep));
		this.add(jspAnneeDep);
	}
	
	/**
	 * Active ou desactive les zone de texte editable.
	 * @param mode si on peu editer les champs false, sinon true
	 */
	public void setConsultationMode(boolean mode){	
		jspJourArr.setEnabled(!mode);
		jspAnneArr.setEnabled(!mode);
		jspJourDep.setEnabled(!mode);
		jspAnneeDep.setEnabled(!mode);
		comboBoxMoisArr.setEnabled(!mode);
		comboBoxMoisDep.setEnabled(!mode);
	}
	
	/**
	 * Les deux methode suivant retourne les dates a partir des informations fournies
	 * @return une date en format Calendar
	 */
	public Calendar getDateArrivee(){
		Calendar tmpDateArr = new GregorianCalendar();
		tmpDateArr.setTimeInMillis(0);
		
		int anneeArrSelectionne =((SpinnerNumberModel)(jspAnneArr.getModel())).getNumber().intValue();
		
		int moisArrSelectionne =comboBoxMoisArr.getSelectedIndex();
		
		int jourArrSelectionne =((SpinnerNumberModel)(jspJourArr.getModel())).getNumber().intValue();
		
		tmpDateArr.set(anneeArrSelectionne,moisArrSelectionne ,jourArrSelectionne );
		
		return tmpDateArr;
	}
	public Calendar getDateDepart(){
		Calendar tmpDateDep = new GregorianCalendar();
		tmpDateDep.setTimeInMillis(0);
		
		int anneeArrSelectionne =((SpinnerNumberModel)(jspAnneeDep.getModel())).getNumber().intValue();
		
		int moisArrSelectionne =comboBoxMoisDep.getSelectedIndex();
		
		int jourArrSelectionne =((SpinnerNumberModel)(jspJourDep.getModel())).getNumber().intValue();
		
		tmpDateDep.set(anneeArrSelectionne,moisArrSelectionne ,jourArrSelectionne );
		
		return tmpDateDep;
	}
	
	/**
	 * Modifie les donnees des dates selon les dates fournies
	 * @param dateArr affecte tous ce qui est Arrivee
	 * @param dateDep affecte tous ce qui est Depart
	 */
	public void insererDates(Date dateArr, Date dateDep){
		Calendar tempValue = new GregorianCalendar();
		
		tempValue.setTime(dateArr);
		modelJourArr = new SpinnerNumberModel(tempValue.get(Calendar.DAY_OF_MONTH), 1, tempValue.getActualMaximum(Calendar.DAY_OF_MONTH),1);
		jspJourArr.setModel(modelJourArr);
		modelAnneeArr = new SpinnerNumberModel(tempValue.get(Calendar.YEAR), cal.get(Calendar.YEAR), 2100, 1);
		jspAnneArr.setModel(modelAnneeArr);
		comboBoxMoisArr.setSelectedIndex(tempValue.get(Calendar.MONTH));
		
		tempValue.setTime(dateDep);
		modelJourDep = new SpinnerNumberModel(tempValue.get(Calendar.DAY_OF_MONTH), 1, tempValue.getActualMaximum(Calendar.DAY_OF_MONTH),1);
		jspJourDep.setModel(modelJourDep);
		modelAnneeDep = new SpinnerNumberModel(tempValue.get(Calendar.YEAR), cal.get(Calendar.YEAR), 2100, 1);
		jspAnneeDep.setModel(modelAnneeDep);
		comboBoxMoisDep.setSelectedIndex(tempValue.get(Calendar.MONTH));
	}
	
	private class DateChangeListener implements ChangeListener{
		private ActionListener ctrl;
		private JSpinner spinner;
		
		public DateChangeListener(ActionListener ctrl, JSpinner spinner){
			this.spinner = spinner;
			this.ctrl = ctrl;
		}
		@Override
		public void stateChanged(ChangeEvent arg0) {
			ctrl.actionPerformed(new ActionEvent(spinner, 1, String.valueOf(JOUR_CHANGE)));
			
		}
	}
}
